import sys
import tomllib

from gradio_client import Client

def attack():
    # Load prompt from configuration
    try:
        with open("config.toml", "rb") as f:
            config = tomllib.load(f)
        prompt = config["attack"]["prompt"]
    except FileNotFoundError:
        print("[!] config.toml not found.")
        sys.exit(1)
    except Exception as e:
        print(f"[!] Error loading config: {e}")
        sys.exit(1)
    
    try:
        print(f"[*] Connecting to Gradio interface at http://localhost:7860...")
        client = Client("http://localhost:7860")
        
        print(f"[*] Sending adversarial prompt: {prompt}")
        result = client.predict(
            message=prompt,
            api_name="/chat",
        )
        
        print(f"[*] Response received:\n{result}")
            
    except Exception as e:
        print(f"[!] Error communicating with API: {e}")
        sys.exit(1)

if __name__ == "__main__":
    attack()
